#pragma once
#pragma warning(disable:4996)       // disable checked iterator errors http://msdn.microsoft.com/en-us/library/aa985965(VS.80).aspx 

//
// Copyright (C) 2011 - 2013 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1

#include <Macros.h>
#include <ValueHelpers.h>
#include <Settings.h>

#pragma unmanaged 
#include <ql\stochasticprocess.hpp>
#include <boost/smart_ptr/detail/spinlock.hpp>
#pragma managed 

using namespace System;
using namespace QuantLib;
using namespace Cephei;
using namespace Cephei::Core;
using namespace PLATFORM::Collections;

using namespace Cephei::QL::Math;
#define HANDLE
#define ABSTRACT
#undef STRUCT
namespace Cephei { namespace QL {
	//////////////////////////////////////////////////////////////////////////////////////////////
	// implementation of IStochasticProcess
	public ref class CStochasticProcess  : 
            public Cephei::QL::IStochasticProcess
	{
	protected: 
		boost::shared_ptr<QuantLib::StochasticProcess>* _ppStochasticProcess;
        boost::detail::spinlock* _pSpinlock;
#ifdef HANDLE
		QuantLib::Handle<QuantLib::StochasticProcess>* _phStochasticProcess;
#endif
		Object^ _StochasticProcessOwner;     // reference to object that manages the storage for this object
	internal:
        CStochasticProcess (boost::shared_ptr<QuantLib::StochasticProcess>& childNative, Object^ owner);
        CStochasticProcess (QuantLib::StochasticProcess& childNative, Object^ owner);
        CStochasticProcess (CStochasticProcess^ copy);
        CStochasticProcess (PLATFORM::Type^ t);
#ifdef STRUCT
        CStochasticProcess (QuantLib::StochasticProcess childNative);
#endif       
#ifdef HANDLE
		CStochasticProcess (QuantLib::Handle<QuantLib::StochasticProcess>& childNative, Object^ owner);
		CStochasticProcess (QuantLib::Handle<QuantLib::StochasticProcess> childNative);
#endif
		virtual ~CStochasticProcess ();
		!CStochasticProcess ();

	internal:
		QuantLib::StochasticProcess& GetReference ();
		boost::shared_ptr<QuantLib::StochasticProcess>& GetShared ();
		QuantLib::StochasticProcess* GetPointer ();
        void SetStochasticProcess (boost::shared_ptr<QuantLib::StochasticProcess> native)
        {
            if (_ppStochasticProcess != NULL)
                delete _ppStochasticProcess;
            _ppStochasticProcess = new boost::shared_ptr<QuantLib::StochasticProcess> (native);
            
        }
#ifdef HANDLE
		QuantLib::Handle<QuantLib::StochasticProcess>& GetHandle ();
#endif
		virtual bool HasNative () ;
		void Lock ()
		{
			if (Cephei::QL::Settings::_ReferenceLocking)
    			_pSpinlock->lock ();
		}
		void Unlock ()
		{
			_pSpinlock->unlock ();
		}
    public:
        property UInt64 Factors 
        {
		    virtual UInt64 get () ;
        }
        property UInt64 Size 
        {
		    virtual UInt64 get () ;
        }
		virtual Double Time (DateTime prm1) ;
        property Cephei::QL::IStochasticProcess^ Update 
        {
		    virtual Cephei::QL::IStochasticProcess^ get () ;
        }
    };
	//////////////////////////////////////////////////////////////////////////////////////////////
	// implementation of IStochasticProcess Vector interface
	ref class CStochasticProcessVector sealed : 
							    public Core::INativeVector<IStochasticProcess^>,
							    public Core::INativeVector<Core::Generic::ICoCell<IStochasticProcess^>^>
	{
	private:
#ifdef STRUCT
		std::vector<QuantLib::StochasticProcess>* _pVValue;
#endif        
		std::vector<boost::shared_ptr<QuantLib::StochasticProcess>>* _pVBoost;
#ifdef HANDLE
		std::vector<QuantLib::Handle<QuantLib::StochasticProcess>>* _pVHand;
#endif
		Object^ _owner;		// object reference to prevent GC of vector owner

	internal:
		CStochasticProcessVector ();
#ifdef STRUCT
		CStochasticProcessVector (std::vector<QuantLib::StochasticProcess>& vec, Object^ owner);
		CStochasticProcessVector (std::vector<QuantLib::StochasticProcess> vec);
#endif       
		CStochasticProcessVector (std::vector<boost::shared_ptr<QuantLib::StochasticProcess>>& vec, Object^ owner);
#ifdef HANDLE
		CStochasticProcessVector (std::vector<QuantLib::Handle<QuantLib::StochasticProcess>>& vec, Object^ owner);
#endif
		virtual ~CStochasticProcessVector ();
		!CStochasticProcessVector ();

    public:
		property int Count 
		{
			virtual int get ();
		}

		property IStochasticProcess^ default[int] 
		{
			virtual IStochasticProcess^ get (int index) = Core::INativeVector<IStochasticProcess^>::default::get;
			virtual void set (int index, IStochasticProcess^ value) = Core::INativeVector<IStochasticProcess^>::default::set;
		}

		//////////////////////////////////////////////////////////////////////////////////////////
		// Interface functions for cell vectors 
	public:
		property Core::Generic::ICoCell<Cephei::QL::IStochasticProcess^>^ CItem[int] 
		{
			virtual Core::Generic::ICoCell<IStochasticProcess^>^ get (int index) = Core::INativeVector<Core::Generic::ICoCell<IStochasticProcess^>^>::default::get;
			virtual void set (int index, Core::Generic::ICoCell<IStochasticProcess^>^ value) = Core::INativeVector<Core::Generic::ICoCell<IStochasticProcess^>^>::default::set;
		}
	internal:
#ifdef STRUCT    
		std::vector<QuantLib::StochasticProcess>& GetReference ();
#endif     
		std::vector<boost::shared_ptr<QuantLib::StochasticProcess>>& GetShared ();

#ifdef HANDLE
		std::vector<QuantLib::Handle<QuantLib::StochasticProcess>>& GetHandle ();
#endif
	public:
		virtual bool PrepareFeature (NativeFeature feature);
	};
    
	//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	// Factory class
//z	[FactoryFor(Core::Generic::ICoCell<Cephei::QL::IStochasticProcess^>::typeid)]
	[FactoryFor(Cephei::QL::IStochasticProcess::typeid)]
	[FactoryFor(Cephei::QL::IStochasticProcess_Factory::typeid)]
	public ref class CStochasticProcess_Factory sealed : public IStochasticProcess_Factory
	{
	public:
        /* <summary> Create a hybrid Vector of IStochasticProcess, with event notification of changes </summary> */
        virtual Cephei::Core::IVector<IStochasticProcess^>^ CreateVector();
        /* <summary> Create a hybrid Vector of ICell of IStochasticProcess, with event notification of changes </summary> */
        virtual Core::Generic::ICoCell<Cephei::Core::IVector<Core::Generic::ICoCell<IStochasticProcess^>^>^>^ CreateCellVector();
        virtual Cephei::Core::IVector<IStochasticProcess^>^ CreateVector(PLATFORM::Collections::Generic::IEnumerable<IStochasticProcess^>^ source);
        virtual Core::Generic::ICoCell<Cephei::Core::IVector<Core::Generic::ICoCell<IStochasticProcess^>^>^>^ CreateCellVector(PLATFORM::Collections::Generic::IEnumerable<Core::Generic::ICoCell<IStochasticProcess^>^>^ source);
		virtual Core::INativeVector<Cephei::QL::IStochasticProcess^>^ CreateNativeVector ()
		{
			return REFNEW CStochasticProcessVector ();
		}

        virtual Core::INativeMatrix<Cephei::QL::IStochasticProcess^>^ CreateNativeMatrix ()
		{
			return nullptr;
		}

        virtual Core::INativeCube<Cephei::QL::IStochasticProcess^>^ CreateNativeCube ()
		{
			return nullptr;
		}
    };
   
/*Cephei*/ } /*QL */}
